package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TaskInfoMapper;
import com.ruoyi.business.domain.TaskInfo;
import com.ruoyi.business.service.ITaskInfoService;

import javax.validation.Validator;

/**
 * 重点工作任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class TaskInfoServiceImpl  extends BaseServiceImpl implements ITaskInfoService
{
    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询重点工作任务
     * 
     * @param id 重点工作任务主键
     * @return 重点工作任务
     */
    @Override
    public TaskInfo selectTaskInfoById(Long id)
    {
        return taskInfoMapper.selectTaskInfoById(id);
    }

    /**
     * 查询重点工作任务列表
     * 
     * @param taskInfo 重点工作任务
     * @return 重点工作任务
     */
    @Override
    public List<TaskInfo> selectTaskInfoList(TaskInfo taskInfo)
    {
        return taskInfoMapper.selectTaskInfoList(taskInfo);
    }

    /**
     * 新增重点工作任务
     * 
     * @param taskInfo 重点工作任务
     * @return 结果
     */
    @Override
    public int insertTaskInfo(TaskInfo taskInfo)
    {
        taskInfo.setCreateTime(DateUtils.getNowDate());
        return taskInfoMapper.insertTaskInfo(taskInfo);
    }

    /**
     * 修改重点工作任务
     * 
     * @param taskInfo 重点工作任务
     * @return 结果
     */
    @Override
    public int updateTaskInfo(TaskInfo taskInfo)
    {
        taskInfo.setUpdateTime(DateUtils.getNowDate());
        return taskInfoMapper.updateTaskInfo(taskInfo);
    }

    /**
     * 批量删除重点工作任务
     * 
     * @param ids 需要删除的重点工作任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskInfoByIds(Long[] ids)
    {
        return taskInfoMapper.deleteTaskInfoByIds(ids);
    }

    /**
     * 删除重点工作任务信息
     * 
     * @param id 重点工作任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskInfoById(Long id)
    {
        return taskInfoMapper.deleteTaskInfoById(id);
    }

    @Override
    public String importData(List<TaskInfo> taskList, boolean updateSupport, String operName) {
    if (taskList == null || taskList.isEmpty()) {
        throw new ServiceException("导入任务信息数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    List<String>failureNums = new ArrayList<>();

    int i = 1;
    for (TaskInfo task : taskList) {
        try {
            i++;
            if (task.getAwardYear() == null || task.getAwardYear().equals("") || task.getEmployeeId() ==null || task.getEmployeeId().equals("") || !isAwardYearValid(task.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }

            // 验证是否存在这个任务
            TaskInfo existingTask = taskInfoMapper.selectTaskInfoById(task.getId());
            if (existingTask == null) {
                // 验证数据
                BeanValidators.validateWithException(validator, task);
                // 设置初始值
                task.setCreateTime(DateUtils.getNowDate());
                task.setCreateBy(operName);
                taskInfoMapper.insertTaskInfo(task);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、任务 "  + " 导入成功");
            } else if (updateSupport) {
                // 验证数据
                BeanValidators.validateWithException(validator, task);
                task.setId(existingTask.getId());
                task.setUpdateTime(DateUtils.getNowDate());
                task.setUpdateBy(operName);
                taskInfoMapper.updateTaskInfo(task);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、任务 " + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、任务 "  + " 已存在");
            }

        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、任务 "  + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }

        setCacheInfo("重点工作任务信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
